// pagesMer/pages/mapPoly/index.js
var app = getApp()
var common_Js = require("../../../component/common.js"), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mer_id:0,
    polygon:[],
    points:[],
    markers:[],
    isLoading: true, 
    loading: true, 
    netWork: true,
    lng:0,
    lat:0,
    controls:[],
    mapCtx:null,
    addId:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.mer_id){
      this.data.mer_id = parseInt(options.mer_id);
    }
    if(options.addId){
      this.data.addId = parseInt(options.addId);
    }
    this.modalcnt()
    // this.mapCtx = wx.createMapContext('map')
  },  
  modalcnt:function  ()  {
    var that = this;
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success:  function  (res)  {
        console.log('modalcnt', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.data.wd = latitude//纬度
        that.data.jd = longitude//经度
        that.setData({
          lat : latitude,
          lng : longitude
        })
        let marker = {
          label:'我的当前位置',
          iconPath:'',
          longitude: longitude,
          latitude: latitude,
          id: 0,
          width: 30,
          height:45,
          callout: {
            content: '我的当前位置',
            color: '#ff0000',
            fontSize: 14,
            borderWidth: 2,
            borderRadius: 10,
            borderColor: '#000000',
            bgColor: '#fff',
            padding: 5,
            display: 'ALWAYS',
            textAlign: 'center'
          },
        }
        that.data.markers.push(marker)
        that.setData({
          markers: that.data.markers
        })
        that.onHttpInit();
      },fail:function(res){
        that.data.lng = 0
        that.data.lat = 0
        that.onHttpInit()
      }
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/mer/detail',{
      mer_id: that.data.mer_id,
      address_id: that.data.addId,
    }, function (res) {
      console.log("获取我的商家详情", res)
      if(res.data.code == 0){
        let points = res.data.data.mer.points;
        points = points.replaceAll('lng','longitude').replaceAll('lat','latitude')
        that.data.points =  JSON.parse(points);
        console.log('that.data.points', that.data.points)
        that.data.polygon.push({
          points: that.data.points,
          strokeWidth: 6,
          strokeColor: "#FF000020",
          fillColor: "rgba(131,177,238, 0.5)",          
        });
        console.log('polygon: ', that.data.polygon)
        that.setData({ 
          lng: res.data.data.mer.longitude,
          lat: res.data.data.mer.latitude,
          polygon: that.data.polygon,
        });
        that.addMarker(res.data.data.mer.id,res.data.data.mer.longitude,res.data.data.mer.latitude,'商家的位置')
        let address_lng = res.data.data.address_lng;
        let address_lat = res.data.data.address_lat;
        
        that.addMarker(99, address_lng, address_lat,'收货地址')
      }
      that.setData({ isLoading: false, loading: true, netWork: true })
    },function(){})
  },
  addMarker(id,lng,lat,title){
    var that = this;
    console.log('that.data.markers', that.data.markers)
    
    let marker = {
      longitude: lng,
      latitude: lat,
      id:  id,
      width: 30,
      height:45,
      callout: {
        content: title,
        color: '#ff0000',
        fontSize: 14,
        borderWidth: 2,
        borderRadius: 10,
        borderColor: '#000000',
        bgColor: '#fff',
        padding: 5,
        display: 'ALWAYS',
        textAlign: 'center'
      },
    }
    that.data.markers.push(marker)
    console.log('that.data.markers == ', that.data.markers)
    that.setData({
      markers: that.data.markers
    })
  },
  regionchange(e) {
    console.log('regionchange' ,e)
  },
  markertap(e) {
    console.log('markertap',e)
  },
  controltap(e) {
    console.log('controltap',e)
  },
  to_ogran:function(){
    console.log('to_ogran',11);
  },
  
})